﻿using System;

namespace CodeStage.AntiCheat.ObscuredTypes
{
    /// <summary>
    /// Use it instead of regular <c>short</c> for any cheating-sensitive variables.
    /// </summary>
    /// <strong><em>Regular type is faster and memory wiser comparing to the obscured one!</em></strong>
    [Serializable]
    public struct ObscuredShort : IEquatable<ObscuredShort>, IFormattable
    {
        private static short cryptoKey = 214;

#if UNITY_EDITOR

        // For internal Editor usage only (may be useful for drawers).
        public static short cryptoKeyEditor = cryptoKey;

#endif

        private short currentCryptoKey;
        private short hiddenValue;
        private short fakeValue;
        private bool inited;

        private ObscuredShort(short value)
        {
            currentCryptoKey = cryptoKey;
            hiddenValue = value;
            fakeValue = 0;
            inited = true;
        }

        /// <summary>
        /// Allows to change default crypto key of this type instances. All new instances will use specified key.<br/>
        /// All current instances will use previous key unless you call ApplyNewCryptoKey() on them explicitly.
        /// </summary>
        public static void SetNewCryptoKey(short newKey)
        {
            cryptoKey = newKey;
        }

        /// <summary>
        /// Use it after SetNewCryptoKey() to re-encrypt current instance using new crypto key.
        /// </summary>
        public void ApplyNewCryptoKey()
        {
            if (currentCryptoKey != cryptoKey)
            {
                hiddenValue = EncryptDecrypt(InternalDecrypt(), cryptoKey);
                currentCryptoKey = cryptoKey;
            }
        }

        /// <summary>
        /// Simple symmetric encryption, uses default crypto key.
        /// </summary>
        /// <returns>Encrypted or decrypted <c>short</c> (depending on what <c>short</c> was passed to the function)</returns>
        public static short EncryptDecrypt(short value)
        {
            return EncryptDecrypt(value, 0);
        }

        /// <summary>
        /// Simple symmetric encryption, uses passed crypto key.
        /// </summary>
        /// <returns>Encrypted or decrypted <c>short</c> (depending on what <c>short</c> was passed to the function)</returns>
        public static short EncryptDecrypt(short value, short key)
        {
            if (key == 0)
            {
                return (short)(value ^ cryptoKey);
            }
            return (short)(value ^ key);
        }

        /// <summary>
        /// Allows to pick current obscured value as is.
        /// </summary>
        /// Use it in conjunction with SetEncrypted().<br/>
        /// Useful for saving data in obscured state.
        public short GetEncrypted()
        {
            ApplyNewCryptoKey();

            return hiddenValue;
        }

        /// <summary>
        /// Allows to explicitly set current obscured value.
        /// </summary>
        /// Use it in conjunction with GetEncrypted().<br/>
        /// Useful for loading data stored in obscured state.
        public void SetEncrypted(short encrypted)
        {
            inited = true;
            hiddenValue = encrypted;
            if (Detectors.ObscuredCheatingDetector.isRunning)
            {
                fakeValue = InternalDecrypt();
            }
        }

        private short InternalDecrypt()
        {
            if (!inited)
            {
                currentCryptoKey = cryptoKey;
                hiddenValue = EncryptDecrypt(0);
                fakeValue = 0;
                inited = true;
            }

            short key = cryptoKey;

            if (currentCryptoKey != cryptoKey)
            {
                key = currentCryptoKey;
            }

            short decrypted = EncryptDecrypt(hiddenValue, key);

            if (Detectors.ObscuredCheatingDetector.isRunning && fakeValue != 0 && decrypted != fakeValue)
            {
                Detectors.ObscuredCheatingDetector.Instance.OnCheatingDetected();
            }

            return decrypted;
        }

        #region operators, overrides, interface implementations

        //! @cond
        public static implicit operator ObscuredShort(short value)
        {
            ObscuredShort obscured = new ObscuredShort(EncryptDecrypt(value));
            if (Detectors.ObscuredCheatingDetector.isRunning)
            {
                obscured.fakeValue = value;
            }
            return obscured;
        }

        public static implicit operator short(ObscuredShort value)
        {
            return value.InternalDecrypt();
        }

        public static ObscuredShort operator ++(ObscuredShort input)
        {
            short decrypted = (short)(input.InternalDecrypt() + 1);
            input.hiddenValue = EncryptDecrypt(decrypted);

            if (Detectors.ObscuredCheatingDetector.isRunning)
            {
                input.fakeValue = decrypted;
            }
            return input;
        }

        public static ObscuredShort operator --(ObscuredShort input)
        {
            short decrypted = (short)(input.InternalDecrypt() - 1);
            input.hiddenValue = EncryptDecrypt(decrypted);

            if (Detectors.ObscuredCheatingDetector.isRunning)
            {
                input.fakeValue = decrypted;
            }
            return input;
        }

        /// <summary>
        /// Returns a value indicating whether this instance is equal to a specified object.
        /// </summary>
        ///
        /// <returns>
        /// true if <paramref name="obj"/> is an instance of ObscuredShort and equals the value of this instance; otherwise, false.
        /// </returns>
        /// <param name="obj">An object to compare with this instance, or null. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (!(obj is ObscuredShort))
                return false;

            ObscuredShort ob = (ObscuredShort)obj;
            return hiddenValue == ob.hiddenValue;
        }

        /// <summary>
        /// Returns a value indicating whether this instance and a specified ObscuredShort object represent the same value.
        /// </summary>
        ///
        /// <returns>
        /// true if <paramref name="obj"/> is equal to this instance; otherwise, false.
        /// </returns>
        /// <param name="obj">An ObscuredShort object to compare to this instance.</param><filterpriority>2</filterpriority>
        public bool Equals(ObscuredShort obj)
        {
            return hiddenValue == obj.hiddenValue;
        }

        /// <summary>
        /// Converts the numeric value of this instance to its equivalent string representation.
        /// </summary>
        ///
        /// <returns>
        /// The string representation of the value of this instance, consisting of a sequence of digits ranging from 0 to 9, without a sign or leading zeroes.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public override string ToString()
        {
            return InternalDecrypt().ToString();
        }

        /// <summary>
        /// Converts the numeric value of this instance to its equivalent string representation using the specified format.
        /// </summary>
        ///
        /// <returns>
        /// The string representation of the value of this instance as specified by <paramref name="format"/>.
        /// </returns>
        /// <param name="format">A numeric format string.</param><exception cref="T:System.FormatException">The <paramref name="format"/> parameter is invalid. </exception><filterpriority>1</filterpriority>
        public string ToString(string format)
        {
            return InternalDecrypt().ToString(format);
        }

        /// <summary>
        /// Returns the hash code for this instance.
        /// </summary>
        ///
        /// <returns>
        /// A hash code for the current ObscuredShort.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return InternalDecrypt().GetHashCode();
        }

        /// <summary>
        /// Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
        /// </summary>
        ///
        /// <returns>
        /// The string representation of the value of this instance , which consists of a sequence of digits ranging from 0 to 9, without a sign or leading zeros.
        /// </returns>
        /// <param name="provider">An object that supplies culture-specific formatting information. </param><filterpriority>1</filterpriority>
        public string ToString(IFormatProvider provider)
        {
            return InternalDecrypt().ToString(provider);
        }

        /// <summary>
        /// Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
        /// </summary>
        ///
        /// <returns>
        /// The string representation of the value of this instance as specified by <paramref name="format"/> and <paramref name="provider"/>.
        /// </returns>
        /// <param name="format">A numeric format string.</param><param name="provider">An object that supplies culture-specific formatting information about this instance. </param><exception cref="T:System.FormatException">The <paramref name="format"/> parameter is invalid. </exception><filterpriority>1</filterpriority>
        public string ToString(string format, IFormatProvider provider)
        {
            return InternalDecrypt().ToString(format, provider);
        }

        //! @endcond

        #endregion operators, overrides, interface implementations
    }
}